﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace HOOMT
{
    public enum Diagram
    {
        NullObject, HighOrderObject, PrimitiveObject, SimpleRelationship, ComplexRelationship,
        HighState, PrimitiveState, StateTransitionFlow,
        Process, InformationSource, InformationFlow,
    }
    //************************************************************************************
    public abstract class OriginalDiagram
    {
        #region 属性
        protected Point upLeftpoint;
        protected int width, height;
        protected bool isSelected;
        protected String name;
        protected TextBox nameBox = new TextBox();//文本框，内容为name包含的字符串
        public String id;
        public int highDiagramNum = 0;//记录高阶对象编号
        //public string diagramNodeFullPath = "123";//记录图形对象路径
        //public List<OriginalDiagram> list;
        private OriginalModelForm modelForm;//记录对象所在的绘制载体（删除高阶对象时使用）
        public TreeNode treeNode { get; set; }//记录对象对应的树节点（只针对高阶对象）
        public SqliteDataBaseOperator dbo;
        private List<OriginalLine> associationStartList = new List<OriginalLine>();

        public  List<OriginalLine> AssociationStartList
        {
            get { return associationStartList; }
            set { associationStartList = value; }
        }
        private List<OriginalLine> associationEndList = new List<OriginalLine>();

        public List<OriginalLine> AssociationEndList
        {
            get { return associationEndList; }
            set { associationEndList = value; }
        }
        
        #endregion
        //###################################################################################
        public OriginalDiagram(Point point, int width, int height)
        {
            this.width = width;
            this.height = height;
            isSelected = true;
            upLeftpoint = new Point(point.X - width / 2, point.Y - height / 2);
            nameBox.TextAlign = HorizontalAlignment.Center;
            nameBox.RightToLeft = RightToLeft.Yes;
            //为NameBox添加鼠标响应函数
            nameBox.MouseDown +=new MouseEventHandler(nameBox_MouseDown);
            this.id = System.Guid.NewGuid().ToString();
          //  Console.WriteLine(id);
        }

        public OriginalDiagram(Point point, int width, int height,string id)
        {
            this.width = width;
            this.height = height;
            isSelected = true;
            upLeftpoint = new Point(point.X - width / 2, point.Y - height / 2);
            nameBox.TextAlign = HorizontalAlignment.Center;
            nameBox.RightToLeft = RightToLeft.Yes;
            //为NameBox添加鼠标响应函数
            nameBox.MouseDown += new MouseEventHandler(nameBox_MouseDown);
            this.id = id;
            //  Console.WriteLine(id);
        }

        //************************************************************************************
        //NameBox鼠标事件响应函数
        private void nameBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Clicks == 2 && e.Button == MouseButtons.Left)
            {
                nameBox.SelectAll();
            }
            else if (e.Button == MouseButtons.Left)
            {
                nameBox.SelectionStart = nameBox.Text.Length;
            }
        }
        //************************************************************************************
        public virtual void Drawing(Graphics graphics, OriginalModelForm m)
        {
            //显示文字
            nameBox.Size = new Size(width, 30);
            nameBox.Location = new Point(upLeftpoint.X, upLeftpoint.Y - 15);
            nameBox.Text = name;
            nameBox.TabStop = false;//删除对象时光标不跳入下一个对象的TextBox
            nameBox.BackColor = SystemColors.Window;//设置为与MainForm相同的颜色
            nameBox.BorderStyle = System.Windows.Forms.BorderStyle.None;//取消边框
            m.Controls.Add(nameBox);
        }
        public virtual int getMinHeight(){return 0;}
        //************************************************************************************
        //把此图像对象从它所在的链表中移除
        public void Remove()
        {
            if (this is HighOrderObjectDiagram)
            {
                HighOrderObjectDiagram diagram = (HighOrderObjectDiagram)this;
                RemoveObjectNextLevel(diagram);
            }
            else if (this is HighStateDiagram)
            {
                HighStateDiagram diagram = (HighStateDiagram)this;
                RemoveStateNextLevel(diagram);
            }
            else if (this is InformationSourceDiagram)
            {
                InformationSourceDiagram diagram = (InformationSourceDiagram)this;
                RemoveInformationNextLevel(diagram);
            }
            modelForm.diagramList.Remove(this);
            Point centerPoint = new Point(this.UpLeftpoint.X + this.Width / 2, this.UpLeftpoint.Y + this.Height / 2);
            modelForm.Controls.Remove(this.NameBox);
            //diagramList.Remove(diag);
            //删除与此图形对象相连接的所有线条
            for (int i = 0; i < modelForm.straightlineList.Count(); i++)
            {
                if (modelForm.straightlineList[i].StartPoint == centerPoint || modelForm.straightlineList[i].EndPoint == centerPoint)
                {
                    modelForm.straightlineList.Remove(modelForm.straightlineList[i]);
                    i--;//调整位置
                }
            }
            modelForm.Invalidate();
            //List.Remove(this);
        }
        //HighOrderObjectDiagram移除
        private void RemoveObjectNextLevel(HighOrderObjectDiagram diagram)
        {
            if (diagram.NewModelForm != null)
            {
                foreach (OriginalDiagram diag in diagram.NewModelForm.diagramList)
                {
                    if (diag is HighOrderObjectDiagram)
                    {
                        HighOrderObjectDiagram dgm = (HighOrderObjectDiagram)diag;
                        RemoveObjectNextLevel(dgm);
                    }
                }
                diagram.NewModelForm.diagramList.Clear();
                diagram.NewModelForm.straightlineList.Clear();
                diagram.NewModelForm.Close();//内存没有释放？？？？
                //diagram.NewModelForm.Dispose();
            }
        }
        //HighStateDiagram移除
        private void RemoveStateNextLevel(HighStateDiagram diagram)
        {
            if (diagram.NewModelForm != null)
            {
                foreach (OriginalDiagram diag in diagram.NewModelForm.diagramList)
                {
                    if (diag is HighStateDiagram)
                    {
                        HighStateDiagram dgm = (HighStateDiagram)diag;
                        RemoveStateNextLevel(dgm);
                    }
                }
                diagram.NewModelForm.diagramList.Clear();
                diagram.NewModelForm.straightlineList.Clear();
                diagram.NewModelForm.Close();//内存没有释放？？？？
                //diagram.NewModelForm.Dispose();
            }
        }
        //InformationSourceDiagram移除
        private void RemoveInformationNextLevel(InformationSourceDiagram diagram)
        {
            if (diagram.NewModelForm != null)
            {
                foreach (OriginalDiagram diag in diagram.NewModelForm.diagramList)
                {
                    if (diag is InformationSourceDiagram)
                    {
                        InformationSourceDiagram dgm = (InformationSourceDiagram)diag;
                        RemoveInformationNextLevel(dgm);
                    }
                }
                diagram.NewModelForm.diagramList.Clear();
                diagram.NewModelForm.straightlineList.Clear();
                diagram.NewModelForm.Close();//内存没有释放？？？？
                //diagram.NewModelForm.Dispose();
            }
        }

        #region 访问器
        public OriginalModelForm ModelForm
        {
            get
            {
                return modelForm;
            }
            set 
            { 
                modelForm = value; 
            } 
        }
        public Point UpLeftpoint
        {
            get
            {
                return upLeftpoint;
            }
            set
            {
                upLeftpoint = value;
            }
        }
        public int Width
        {
            get
            {
                return width;
            }
            set
            {
                width = value;
            }
        }
        public int Height
        {
            get
            {
                return height;
            }
            set
            {
                height = value;
            }
        }
        public bool IsSelected
        {
            get
            {
                return isSelected;
            }
            set
            {
                isSelected = value;
            }
        }
        public String Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public String Id
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }
        public TextBox NameBox
        {
            get
            {
                return nameBox;
            }
        }
        #endregion

        //************************************************************************************

        /*public static Point GetCenterPoint(Point upLeftpoint, int width, int height)
        {
            return new Point(upLeftpoint.X + width / 2, upLeftpoint.Y + height / 2);
        }
        public abstract List<Attribute> GetAttributes();
        public abstract List<Method> GetMethods();*/
    }
}
